home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cocktail / rex.lha / rex / m2c / Tree.c < prev    next >
C/C++ Source or Header  |  1992-08-18  |  8KB  |  348 lines

  1. #include "SYSTEM_.h"
  2.  
  3. #ifndef DEFINITION_Memory
  4. #include "Memory.h"
  5. #endif
  6.  
  7. #ifndef DEFINITION_Texts
  8. #include "Texts.h"
  9. #endif
  10.  
  11. #ifndef DEFINITION_Sets
  12. #include "Sets.h"
  13. #endif
  14.  
  15. #ifndef DEFINITION_Strings
  16. #include "Strings.h"
  17. #endif
  18.  
  19. #ifndef DEFINITION_StringMem
  20. #include "StringMem.h"
  21. #endif
  22.  
  23. #ifndef DEFINITION_IO
  24. #include "IO.h"
  25. #endif
  26.  
  27. #ifndef DEFINITION_Layout
  28. #include "Layout.h"
  29. #endif
  30.  
  31. #ifndef DEFINITION_Positions
  32. #include "Positions.h"
  33. #endif
  34.  
  35. #ifndef DEFINITION_Strings
  36. #include "Strings.h"
  37. #endif
  38.  
  39. #ifndef DEFINITION_Tree
  40. #include "Tree.h"
  41. #endif
  42.  
  43.  
  44. static SHORTCARD indent;
  45.  
  46.  
  47. Tree_tTree Tree_MakeTree1
  48. # ifdef __STDC__
  49. (SHORTCARD pRule, Tree_tTree pSon1)
  50. # else
  51. (pRule, pSon1)
  52. SHORTCARD pRule;
  53. Tree_tTree pSon1;
  54. # endif
  55. {
  56.   Tree_tTree Tree;
  57.  
  58.   Tree = (Tree_tTree)Memory_Alloc((LONGINT)sizeof(Tree_Node1));
  59.   {
  60.     register Tree_Node1 *W_1 = &Tree->U_1.V_2.vNode1;
  61.  
  62.     W_1->Rule = pRule;
  63.     W_1->Son1 = pSon1;
  64.   }
  65.   return Tree;
  66. }
  67.  
  68. Tree_tTree Tree_MakeTree2
  69. # ifdef __STDC__
  70. (SHORTCARD pRule, Tree_tTree pSon1, Tree_tTree pSon2)
  71. # else
  72. (pRule, pSon1, pSon2)
  73. SHORTCARD pRule;
  74. Tree_tTree pSon1, pSon2;
  75. # endif
  76. {
  77.   Tree_tTree Tree;
  78.  
  79.   Tree = (Tree_tTree)Memory_Alloc((LONGINT)sizeof(Tree_Node2));
  80.   {
  81.     register Tree_Node2 *W_2 = &Tree->U_1.V_3.vNode2;
  82.  
  83.     W_2->Rule = pRule;
  84.     W_2->Son1 = pSon1;
  85.     W_2->Son2 = pSon2;
  86.   }
  87.   return Tree;
  88. }
  89.  
  90. Tree_tTree Tree_MakeTreeCh
  91. # ifdef __STDC__
  92. (SHORTCARD pRule, CHAR pCh)
  93. # else
  94. (pRule, pCh)
  95. SHORTCARD pRule;
  96. CHAR pCh;
  97. # endif
  98. {
  99.   Tree_tTree Tree;
  100.  
  101.   Tree = (Tree_tTree)Memory_Alloc((LONGINT)sizeof(Tree_NodeCh));
  102.   {
  103.     register Tree_NodeCh *W_3 = &Tree->U_1.V_4.vNodeCh;
  104.  
  105.     W_3->Rule = pRule;
  106.     W_3->Ch = pCh;
  107.   }
  108.   return Tree;
  109. }
  110.  
  111. Tree_tTree Tree_MakeTreeSet
  112. # ifdef __STDC__
  113. (SHORTCARD pRule, Sets_tSet pSet)
  114. # else
  115. (pRule, pSet)
  116. SHORTCARD pRule;
  117. Sets_tSet pSet;
  118. # endif
  119. {
  120.   Tree_tTree Tree;
  121.  
  122.   Tree = (Tree_tTree)Memory_Alloc((LONGINT)sizeof(Tree_NodeSet));
  123.   {
  124.     register Tree_NodeSet *W_4 = &Tree->U_1.V_5.vNodeSet;
  125.  
  126.     W_4->Rule = pRule;
  127.     W_4->Set = pSet;
  128.   }
  129.   return Tree;
  130. }
  131.  
  132. Tree_tTree Tree_MakeTreeString
  133. # ifdef __STDC__
  134. (SHORTCARD pRule, StringMem_tStringRef pString)
  135. # else
  136. (pRule, pString)
  137. SHORTCARD pRule;
  138. StringMem_tStringRef pString;
  139. # endif
  140. {
  141.   Tree_tTree Tree;
  142.  
  143.   Tree = (Tree_tTree)Memory_Alloc((LONGINT)sizeof(Tree_NodeString));
  144.   {
  145.     register Tree_NodeString *W_5 = &Tree->U_1.V_6.vNodeString;
  146.  
  147.     W_5->Rule = pRule;
  148.     W_5->String = pString;
  149.   }
  150.   return Tree;
  151. }
  152.  
  153. Tree_tTree Tree_MakeTreeRule
  154. # ifdef __STDC__
  155. (SHORTCARD pRule, Tree_tTree pPatterns, Texts_tText pTargetCode, SHORTCARD pLine, SHORTCARD pCodeMode, SHORTCARD pRuleNr)
  156. # else
  157. (pRule, pPatterns, pTargetCode, pLine, pCodeMode, pRuleNr)
  158. SHORTCARD pRule;
  159. Tree_tTree pPatterns;
  160. Texts_tText pTargetCode;
  161. SHORTCARD pLine, pCodeMode, pRuleNr;
  162. # endif
  163. {
  164.   Tree_tTree Tree;
  165.  
  166.   Tree = (Tree_tTree)Memory_Alloc((LONGINT)sizeof(Tree_NodeRule));
  167.   {
  168.     register Tree_NodeRule *W_6 = &Tree->U_1.V_7.vNodeRule;
  169.  
  170.     W_6->Rule = pRule;
  171.     W_6->Patterns = pPatterns;
  172.     W_6->TargetCode = pTargetCode;
  173.     W_6->Line = pLine;
  174.     W_6->CodeMode = pCodeMode;
  175.     W_6->RuleNr = pRuleNr;
  176.   }
  177.   return Tree;
  178. }
  179.  
  180. Tree_tTree Tree_MakeTreePattern
  181. # ifdef __STDC__
  182. (SHORTCARD pRule, Sets_tSet pStartStates, Tree_tTree pRegExpr, Tree_tTree pRightContext, BOOLEAN pIsConstantRE, SHORTCARD pPatternNr, Positions_tPosition pPosition)
  183. # else
  184. (pRule, pStartStates, pRegExpr, pRightContext, pIsConstantRE, pPatternNr, pPosition)
  185. SHORTCARD pRule;
  186. Sets_tSet pStartStates;
  187. Tree_tTree pRegExpr, pRightContext;
  188. BOOLEAN pIsConstantRE;
  189. SHORTCARD pPatternNr;
  190. Positions_tPosition pPosition;
  191. # endif
  192. {
  193.   Tree_tTree Tree;
  194.  
  195.   Tree = (Tree_tTree)Memory_Alloc((LONGINT)sizeof(Tree_NodePattern));
  196.   {
  197.     register Tree_NodePattern *W_7 = &Tree->U_1.V_8.vNodePattern;
  198.  
  199.     W_7->Rule = pRule;
  200.     W_7->StartStates = pStartStates;
  201.     W_7->RegExpr = pRegExpr;
  202.     W_7->RightContext = pRightContext;
  203.     W_7->IsConstantRE = pIsConstantRE;
  204.     W_7->PatternNr = pPatternNr;
  205.     W_7->Position = pPosition;
  206.   }
  207.   return Tree;
  208. }
  209.  
  210. void Tree_WriteTree
  211. # ifdef __STDC__
  212. (Tree_tTree t)
  213. # else
  214. (t)
  215. Tree_tTree t;
  216. # endif
  217. {
  218.   SHORTCARD i;
  219.   Strings_tString string;
  220.  
  221.   {
  222.     SHORTCARD B_1 = 1, B_2 = indent;
  223.  
  224.     if (B_1 <= B_2)
  225.       for (i = B_1;; i += 1) {
  226.         Layout_WriteSpace((System_tFile)IO_StdOutput);
  227.         if (i >= B_2) break;
  228.       }
  229.   }
  230.   if (t == Tree_NoTree) {
  231.     IO_WriteS((System_tFile)IO_StdOutput, (STRING)"NoTree", 6L);
  232.     IO_WriteNl((System_tFile)IO_StdOutput);
  233.     return;
  234.   }
  235.   switch (t->U_1.V_1.vNode0.Rule) {
  236.   case Tree_nRule:;
  237.     IO_WriteS((System_tFile)IO_StdOutput, (STRING)"Rule ", 5L);
  238.     IO_WriteI((System_tFile)IO_StdOutput, (LONGINT)t->U_1.V_7.vNodeRule.RuleNr, 1L);
  239.     IO_WriteNl((System_tFile)IO_StdOutput);
  240.     Texts_WriteText((System_tFile)IO_StdOutput, t->U_1.V_7.vNodeRule.TargetCode);
  241.     break;
  242.   case Tree_nList:;
  243.     IO_WriteS((System_tFile)IO_StdOutput, (STRING)"List", 4L);
  244.     IO_WriteNl((System_tFile)IO_StdOutput);
  245.     break;
  246.   case Tree_nPattern:;
  247.     IO_WriteS((System_tFile)IO_StdOutput, (STRING)"Pattern ", 8L);
  248.     Sets_WriteSet((System_tFile)IO_StdOutput, t->U_1.V_8.vNodePattern.StartStates);
  249.     Layout_WriteSpace((System_tFile)IO_StdOutput);
  250.     IO_WriteB((System_tFile)IO_StdOutput, t->U_1.V_8.vNodePattern.IsConstantRE);
  251.     IO_WriteI((System_tFile)IO_StdOutput, (LONGINT)t->U_1.V_8.vNodePattern.PatternNr, 5L);
  252.     IO_WriteNl((System_tFile)IO_StdOutput);
  253.     break;
  254.   case Tree_nAlternative:;
  255.     IO_WriteS((System_tFile)IO_StdOutput, (STRING)"Alternative", 11L);
  256.     IO_WriteNl((System_tFile)IO_StdOutput);
  257.     break;
  258.   case Tree_nSequence:;
  259.     IO_WriteS((System_tFile)IO_StdOutput, (STRING)"Sequence", 8L);
  260.     IO_WriteNl((System_tFile)IO_StdOutput);
  261.     break;
  262.   case Tree_nRepetition:;
  263.     IO_WriteS((System_tFile)IO_StdOutput, (STRING)"Repetition", 10L);
  264.     IO_WriteNl((System_tFile)IO_StdOutput);
  265.     break;
  266.   case Tree_nOption:;
  267.     IO_WriteS((System_tFile)IO_StdOutput, (STRING)"Option", 6L);
  268.     IO_WriteNl((System_tFile)IO_StdOutput);
  269.     break;
  270.   case Tree_nChar:;
  271.     IO_WriteS((System_tFile)IO_StdOutput, (STRING)"Char ", 5L);
  272.     Layout_WriteChar((System_tFile)IO_StdOutput, t->U_1.V_4.vNodeCh.Ch);
  273.     IO_WriteNl((System_tFile)IO_StdOutput);
  274.     break;
  275.   case Tree_nSet:;
  276.     IO_WriteS((System_tFile)IO_StdOutput, (STRING)"Set ", 4L);
  277.     Sets_WriteSet((System_tFile)IO_StdOutput, t->U_1.V_5.vNodeSet.Set);
  278.     IO_WriteNl((System_tFile)IO_StdOutput);
  279.     break;
  280.   case Tree_nString:;
  281.     IO_WriteS((System_tFile)IO_StdOutput, (STRING)"String ", 7L);
  282.     StringMem_GetString(t->U_1.V_6.vNodeString.String, &string);
  283.     Strings_WriteS((System_tFile)IO_StdOutput, &string);
  284.     IO_WriteNl((System_tFile)IO_StdOutput);
  285.     break;
  286.   }
  287.   if (t->U_1.V_1.vNode0.Rule != Tree_nList) {
  288.     INC1(indent, 2);
  289.   }
  290.   switch (t->U_1.V_1.vNode0.Rule) {
  291.   case Tree_nRule:;
  292.     Tree_WriteTree(t->U_1.V_7.vNodeRule.Patterns);
  293.     break;
  294.   case Tree_nList:;
  295.     Tree_WriteTree(t->U_1.V_3.vNode2.Son1);
  296.     Tree_WriteTree(t->U_1.V_3.vNode2.Son2);
  297.     break;
  298.   case Tree_nPattern:;
  299.     Tree_WriteTree(t->U_1.V_8.vNodePattern.RegExpr);
  300.     Tree_WriteTree(t->U_1.V_8.vNodePattern.RightContext);
  301.     break;
  302.   case Tree_nAlternative:;
  303.     Tree_WriteTree(t->U_1.V_3.vNode2.Son1);
  304.     Tree_WriteTree(t->U_1.V_3.vNode2.Son2);
  305.     break;
  306.   case Tree_nSequence:;
  307.     Tree_WriteTree(t->U_1.V_3.vNode2.Son1);
  308.     Tree_WriteTree(t->U_1.V_3.vNode2.Son2);
  309.     break;
  310.   case Tree_nRepetition:;
  311.     Tree_WriteTree(t->U_1.V_2.vNode1.Son1);
  312.     break;
  313.   case Tree_nOption:;
  314.     Tree_WriteTree(t->U_1.V_2.vNode1.Son1);
  315.     break;
  316.   default :
  317.     break;
  318.   }
  319.   if (t->U_1.V_1.vNode0.Rule != Tree_nList) {
  320.     DEC1(indent, 2);
  321.   }
  322. }
  323.  
  324. void BEGIN_Tree()
  325. {
  326.   static BOOLEAN has_been_called = FALSE;
  327.  
  328.   if (!has_been_called) {
  329.     has_been_called = TRUE;
  330.  
  331.     BEGIN_Sets();
  332.     BEGIN_Texts();
  333.     BEGIN_StringMem();
  334.     BEGIN_Positions();
  335.     BEGIN_Memory();
  336.     BEGIN_Texts();
  337.     BEGIN_Sets();
  338.     BEGIN_Strings();
  339.     BEGIN_StringMem();
  340.     BEGIN_IO();
  341.     BEGIN_Layout();
  342.     BEGIN_Positions();
  343.     BEGIN_Strings();
  344.  
  345.     indent = 0;
  346.   }
  347. }
  348.